package org.railage.client.beans;

import java.text.SimpleDateFormat;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;
import org.railage.common.dto.UserDTO;
import org.railage.server.services.UserService;

@ManagedBean(name = "auth")
@SessionScoped
public class AuthBean {
	public static final String SESSION_USER_PARAM = "user";
	
	private static final Logger log = Logger.getLogger(AuthBean.class);
	
	private String username;
	private String password;
	
	public String login() {
		log.debug("Login: " + username);
		log.debug("Password: " + password);
		UserDTO user = new UserService().authenticate(new String[] {username, password});
		if (user == null) {
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_ERROR,
							"Incorrect login or password!", ""));
			return null;
		} else {
			FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(SESSION_USER_PARAM, user);
			if (user.getRole() == 0) {
				return "client/trip-search.xhtml?faces-redirect=true";
			} else if (user.getRole() == 1) {
				return "manager/passengers.xhtml?faces-redirect=true";
			} else {
				return null;
			}
		}
	}
	
	public String logout() {
	    FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove(SESSION_USER_PARAM);
	    return "/auth.xhtml?faces-redirect=true";
	}
	
	public boolean isLoggedIn() {
	    return FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(SESSION_USER_PARAM) != null;
	}
	
	public int getRole() {
		if (isLoggedIn()) {
			UserDTO user = (UserDTO) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(SESSION_USER_PARAM);
			return user.getRole();
		} else {
			return -1;
		}
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}
	
	public String getFIO() {
		UserDTO user = (UserDTO) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(SESSION_USER_PARAM);
		StringBuilder result = new StringBuilder();
		result.append(user.getName()).append(" ").append(user.getSurname()).append(" (");
		if (user.getRole() == 0) {
			result.append("User");
		} else if (user.getRole() == 1) {
			result.append("Manager");
		}
		result.append(")");
		return  result.toString();
	}
	
	public String getName() {
		UserDTO user = (UserDTO) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(SESSION_USER_PARAM);
		return user.getName();
	}
	
	public String getSurname() {
		UserDTO user = (UserDTO) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(SESSION_USER_PARAM);
		return user.getSurname();
	}
	
	public String getBirthday() {
		UserDTO user = (UserDTO) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(SESSION_USER_PARAM);
		SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
		return dateFormatter.format(user.getBirthday());
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

}
